﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FangDuShi_Front.Common
{
    public class IDCard
    {
        /// <summary>
        /// 身份证校验
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool CheckCardId(string id)
        {
            if (id.Length == 18)
            {
                int[] wQuan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                string checkWei = "10X98765432";
                string number17 = id.Substring(0, 17);
                string number18 = id.Substring(17);
                int sum = 0;
                for (int i = 0; i < 17; i++)
                {
                    sum = sum + Convert.ToInt32(number17[i].ToString()) * wQuan[i];
                }
                int mod = sum % 11;
                string result = checkWei[mod].ToString();
                if (number18.Equals(result, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 身份证15位转18位
        /// </summary>
        /// <param name="idCardNo"></param>
        /// <returns></returns>
        public static string IDCard15To18(string idCardNo)
        {
            string _idCardNo = "";
            if (idCardNo.Length == 15)
            {
                int[] wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
                char[] ai = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
                string ls_re = "";
                ls_re = idCardNo.Substring(0, 6) + "19" + idCardNo.Substring(6, 9);
                int ll_sum = 0;
                char ll_code = ' ';
                for (int i = 0; i < 17; i++)
                {
                    ll_sum += wi[i] * int.Parse(ls_re.Substring(i, 1));
                    ll_code = ai[ll_sum % 11];
                }
                ls_re += ll_code;

                _idCardNo = ls_re;
            }
            return _idCardNo;

        }

        /// <summary>
        /// 身份证18位转15位
        /// </summary>
        /// <param name="idCardNo"></param>
        /// <returns></returns>
        public static string IDCard18To15(string idCardNo)
        {
            string _idCardNo = "";
            if (idCardNo.Length == 18)
            {
                string ls_re = "";
                ls_re = idCardNo.Substring(0, 6) + idCardNo.Substring(8, 9);
                _idCardNo = ls_re;
            }
            return _idCardNo;

        }
        #region 第二代身份证读值方法
        /// <summary>
        /// 姓名
        /// </summary>
        /// <returns></returns>
        public static string GetName()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetPeopleName(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        /// <summary>
        /// 性别
        /// </summary>
        /// <returns></returns>
        public static string GetSex()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetPeopleSex(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        /// <summary>
        /// 民族
        /// </summary>
        /// <returns></returns>
        public static string GetNation()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetPeopleNation(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }

        /// <summary>
        /// 出生日期
        /// </summary>
        /// <returns></returns>
        public static string GetBirthday()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];

            int dwRet = Common.ImportDllApiUtil.GetPeopleBirthday(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");

            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);

        }
        /// <summary>
        /// 地址
        /// </summary>
        /// <returns></returns>
        public static string GetAddress()
        {

            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetPeopleAddress(asciiBytes, 100);

            Encoding gb2312 = Encoding.GetEncoding("gb2312");

            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);

        }
        /// <summary>
        /// 得到发证机关信息
        /// </summary>
        /// <returns></returns>
        public static string GetDepartmentIC()
        {

            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetDepartment(asciiBytes, 100);

            Encoding gb2312 = Encoding.GetEncoding("gb2312");

            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }
        /// <summary>
        /// 有效开始日期
        /// </summary>
        /// <returns></returns>
        public static string GetStartDateIC()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetStartDate(asciiBytes, 100);

            Encoding gb2312 = Encoding.GetEncoding("gb2312");

            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);

            string chars = new string(asciiChars);
            string returnValue = chars.Substring(0, 4) + "-" + chars.Substring(4, 2) + "-" + chars.Substring(6, 2);

            return returnValue;

        }

        /// <summary>
        /// 结束日期
        /// </summary>
        /// <returns></returns>
        public static string GetEndDateIC()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetEndDate(asciiBytes, 100);

            Encoding gb2312 = Encoding.GetEncoding("gb2312");

            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);

        }

        /// <summary>
        /// 获得卡号信息
        /// </summary>
        /// <returns></returns>
        public static string GetIDCode()
        {
            Byte[] asciiBytes = null;
            asciiBytes = new Byte[100];
            int dwRet = Common.ImportDllApiUtil.GetPeopleIDCode(asciiBytes, 100);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, dwRet)];
            gb2312.GetChars(asciiBytes, 0, dwRet, asciiChars, 0);
            return new string(asciiChars);
        }
        /// <summary>
        /// 读取照片二进制
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static byte[] GetPhotoBMP()
        {
            string filePath = "zp.bmp";
            Byte[] fileByte = null;
            if (File.Exists(filePath))
            {
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);
                try
                {
                    fileByte = new Byte[(int)fs.Length];
                    fs.Read(fileByte, 0, fileByte.Length);
                    fs.Close();
                }
                catch
                {
                    
                }
                finally
                {
                    fs.Close();
                }
                
            }
            return fileByte;
        }

        #endregion

    }
}
